#!/usr/bin/perl -w
use strict;
use Getopt::Long;

my ($appliance, $collection, $date, $frontend, $action);
GetOptions ("appliance=s"   => \$appliance,     ## GSA Name
            "collection=s"  => \$collection,    ## Name of the collection
            "frontend=s"    => \$frontend,
            "action=s"      => \$action,
            "date=s"        => \$date,          ## date for which the action needs to be performed
            ) 
or die("Error in command line arguments\n");

## Validations ...

if (!($date)) {
    print "Please provide a valid date to continue \n";
    exit;
}

if (!($collection)) {
    print "Please provide a Collection Name to continue \n";
    exit;
}

if (!($frontend)) {
    print "Please provide a Frontend to continue \n";
    exit;
}

if (!($action)) {
    print "Please choose an action (scrub / load) to continue \n";
    exit;
}else{
    if ($action eq 'scrub') {    
        my $logFile = "../output/searchLog/logger_$date";
        if (!(-f "../output/searchLog/logger_$date")) {
            #code
            print "Unable to find file logger_$date at location ../output/searchLog/ \n";
            print "Please download a report for $date and continue again \n";
            exit;
        }
        
        print "extracting query terms \n";
        
        open(FILE, "<$logFile") or die "Cannot open file $logFile for reason $! \n";
        open(SCRUB, ">$logFile.scrub") or die "Cannot open file $logFile.scrub for reason $! \n";
        while (<FILE>) {
            my $string = $_;
            my @split_string = split"&", $string;
            foreach my $i (@split_string){
                if ($i =~ m/^q=/) {
                    $i =~ s/q=//;
                    my @query = split'\+-inmeta', $i;
                    $i = $query[0];
                    $i =~ s/\+/' '/;
                    print "Query term :: $i\n";
                    print SCRUB "$i\::search?site=$collection&client=$frontend&q=$i&output=xml_no_dtd&proxystylesheet=$frontend&access=a\n";
                    last;
                }else{
                    next;
                }
            }
        }
        close SCRUB;
        close FILE;
        
    }elsif ($action eq 'load'){        
        my $logFile = "../output/searchLog/logger_$date.scrub";
        if (!(-f "../output/searchLog/logger_$date.scrub")) {
            print "Unable to find file logger_$date.scrub at location ../output/searchLog/ \n";
            exit;
        }else{
            if (!(-f "../gsa/$appliance")) {
                print "Unable to identify GSA \n";
                exit;
            }
            
            open(FILE, "<../gsa/$appliance") or die "cannot open file \"../gsa/$appliance\" for reason $!";
                my @string = <FILE>;
            close FILE;
            
            my ($protocol, $port, $FQDN, $username, $password);
            
            my $string = join "", @string;
            eval ($string);
            
            my $appliance_url   = "$protocol://$appliance.$FQDN";            
            
            use LWP::UserAgent;            
            use CGI;
            my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 });
           
            ## Get cookie already set on browser.
            my $q = new CGI;
            my $value = $q->cookie('name');
            
            $ua->cookie_jar({ 'name'=> $value }); 
              
            open(LOGGER, "<$logFile") or die "Cannot open $logFile for reason $!";
            my @URLS = <LOGGER>;

            foreach my $i (@URLS){
                my @i = split "::", $i;
                $i = "$appliance_url/$i[1]";
                print "$i \t";

                my $req = HTTP::Request->new(GET => $i);
                my $res = $ua->request($req);
                # Check the outcome of the response
                if (!($res->is_success)){
                    print $res->content, "\t";
                    print $res->status_line, "\n";
                }
            }
            close LOGGER;
        }        
    }
}